package com.finchina.myview;

import android.content.Context;
import android.content.res.TypedArray;
import android.gesture.GestureOverlayView;
import android.util.AttributeSet;
import android.util.Log;
import android.view.GestureDetector;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.RelativeLayout;

/**
 * 作者：黄赛赛 on 2015/9/7 14:56
 *
 * @version V1.0
 * @Description: ${todo}()
 */
public class MyListView extends ListView implements View.OnTouchListener, GestureDetector.OnGestureListener{

    private GestureDetector gestureDetector;

//    private OnDeleteListener listener;

    private View deleteButton;

    private ViewGroup itemLayout;

    private int selectedItem;

    private boolean isDeleteShown;

    public MyListView(Context context) {
        super(context);
    }

    public MyListView(Context context, AttributeSet attrs) {
        super(context, attrs);

        init();
    }

    public MyListView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
//        TypedArray a = context.getTheme().obtainStyledAttributes(attrs,R.styleable.MyListView,defStyleAttr,0);
//        a.getDimension(R.styleable.MyListView_tile_width,0);
//        a.recycle();
    }

    void init (){
        gestureDetector = new GestureDetector(this);
        setOnTouchListener(this);
    }



    @Override
    public boolean onTouch(View v, MotionEvent event) {
        if (isDeleteShown){
            itemLayout.removeView(deleteButton);
            deleteButton = null;
            isDeleteShown = false;
            return false;
        }else {
            return gestureDetector.onTouchEvent(event);
        }

    }

    @Override
    public boolean onDown(MotionEvent e) {
        if (!isDeleteShown ){
            selectedItem = pointToPosition((int)e.getX(),(int) e.getY());
        }
        return false;
    }

    @Override
    public void onShowPress(MotionEvent e) {

    }

    @Override
    public boolean onSingleTapUp(MotionEvent e) {
        return false;
    }

    @Override
    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
        return false;
    }

    @Override
    public void onLongPress(MotionEvent e) {

    }

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
        if (!isDeleteShown && Math.abs(velocityX) > Math.abs(velocityY)){
            deleteButton = LayoutInflater.from(getContext()).inflate(R.layout.btn_del, null);
            itemLayout = (ViewGroup) getChildAt(selectedItem - getFirstVisiblePosition());
            RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT  );
            layoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
            layoutParams.addRule(RelativeLayout.CENTER_VERTICAL);
            itemLayout.addView(deleteButton,layoutParams);
            isDeleteShown = true;
            deleteButton.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    Log.e("deleteButton", "position = " + selectedItem);
                }
            });
        }
        return false;
    }
}
